package Java7_Concurrency_Cookbook.thread_executor_framework.cached_thread_pool;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author lixiangke
 * @date 2017/11/17
 */
public class Server {
    private ThreadPoolExecutor executor;

    public Server() {
        this.executor = (ThreadPoolExecutor)Executors.newCachedThreadPool();
    }

    public void executeTask(Task task) {
        //System.out.printf("Server: A new task has arrived \n");
        executor.execute(task);
        System.out.printf("Server: Pool size: %d\n", executor.getPoolSize());
        //System.out.printf("Server: Active count: %d\n", executor.getActiveCount());
        //System.out.printf("Server: Completed Tasks: %d\n", executor.getCompletedTaskCount());
    }

    public void endServer() {
        executor.shutdown();
    }
}
